package bsc.war.ajax;



import bsc.ejb.entity.ColorimetrySettings;
import bsc.ejb.entity.Dimension;
import bsc.ejb.entity.DimensionMember;
import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.Settings;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.Unit;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class DesignerAddPointer extends AbstractController  {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
         HttpServletResponse response) throws Exception {
        
        try {
            Boolean edit = "edit".equals(request.getParameter("type"));
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idScoreCard = edit && NumberUtils.isNumber(request.getParameter("id")) ? Long.parseLong(request.getParameter("id")) : null;
            Pointer scoreCard = (Pointer)delegate.findScoreCard(idScoreCard);
            List<Person> persons = delegate.findAvaliablePerson();
            List<Dimension> dimensions = delegate.findDimension();
            Framework framework = delegate.findFramework(Boolean.TRUE);
            List<Colorimetry> colorimetries = delegate.findColorimetry(framework, Boolean.TRUE);

            Settings.Frecuency frecuencies[] = Settings.Frecuency.values();
            Settings.Command commands[]= Settings.Command.values();
            Settings.Polarity polarities[]= Settings.Polarity.values();
            Settings.Temporality temporalities[]= Settings.Temporality.values();
            Settings.OperationData operations[]= Settings.OperationData.values();

            //GET Units
            List<Unit> units = delegate.findUnit(framework, Boolean.TRUE);

            if("edit".equals(request.getParameter("type")))
                request.setAttribute("edit",Boolean.TRUE);

            HashMap<Colorimetry, Double> minColorValue = new HashMap<Colorimetry, Double>();
            HashMap<Colorimetry, Double> maxColorValue = new HashMap<Colorimetry, Double>();
            if(scoreCard != null)
            {
                List<ColorimetrySettings> settings = delegate.findColorimetrySettings(scoreCard);
                for( ColorimetrySettings setting : settings )
                {
                    minColorValue.put(setting.getColorimetry(), setting.getMinScore()*100);
                    maxColorValue.put(setting.getColorimetry(), setting.getMaxScore()*100);
                }
            }
            else
            {
                for( Colorimetry colorimetry : colorimetries )
                {
                    minColorValue.put(colorimetry, null);
                    maxColorValue.put(colorimetry, null);
                }
            }

            HashMap<Dimension, Boolean> checkedDimensions = new HashMap<Dimension, Boolean>();
            for( Dimension d : dimensions )
                checkedDimensions.put(d, Boolean.FALSE);
            if(edit)
            {
                List<Dimension> selected = delegate.findDimension(scoreCard);
                for( Dimension d : selected )
                    checkedDimensions.put(d, Boolean.TRUE);
            }

            HashMap<DimensionMember, Boolean> checkedDimensionMembers = new HashMap<DimensionMember, Boolean>();
            List<List<DimensionMember>> dimensionMembers = edit ?
                ScoreCardUtils.getCrossProduct(delegate, delegate.findDimension(scoreCard)) : null;
            if(dimensionMembers != null && dimensionMembers.size() > 0)
            for(List<DimensionMember> list : dimensionMembers)
                for(DimensionMember member : list)
                    checkedDimensionMembers.put(member, Boolean.FALSE);
            List<DimensionalCut> dCuts = edit ? delegate.findDimensionalCut(scoreCard) : null;
            if(dCuts != null && dCuts.size() > 0)
            for(DimensionalCut tmp : dCuts)
            {
                List<DimensionMember> dCutMembers = delegate.findDimensionMember(tmp);
                for(DimensionMember member : dCutMembers)
                    checkedDimensionMembers.put(member, Boolean.TRUE);
            }


            List<ScoreCard> scoreCards =  delegate.findScoreCard();
            List<ScoreCard> pointers = new ArrayList();
            for (ScoreCard scoreCard1 : scoreCards) {
                    if(scoreCard1 instanceof Pointer){
                        pointers.add(scoreCard1);
                    }
            }

            request.setAttribute("edit", edit);
            request.setAttribute("dimensionMembers", dimensionMembers);
            request.setAttribute("checkedDimensionMembers", checkedDimensionMembers);

            request.setAttribute("checkedDimensions", checkedDimensions);
            request.setAttribute("minColorValue", minColorValue);
            request.setAttribute("maxColorValue", maxColorValue);

            request.setAttribute("scoreCard", scoreCard);
            request.setAttribute("persons", persons);
            request.setAttribute("dimensions", dimensions);
            request.setAttribute("colorimetries", colorimetries);

            request.setAttribute("frecuencies", frecuencies);
            request.setAttribute("commands", commands);
            request.setAttribute("polarities", polarities);
            request.setAttribute("temporalities", temporalities);
            request.setAttribute("operations", operations);

            request.setAttribute("scoreCardUnit", scoreCard != null && scoreCard.getUnit() != null ? scoreCard.getUnit() : null);
            request.setAttribute("units", units);

            //System.out.println("el tamaño de pointers es : " + pointers.size());
            request.setAttribute("pointers", pointers);

        } catch (Exception e) {
            System.err.println("Caught exception: "+e);
            e.printStackTrace();
        }
         
         return new ModelAndView("designerAddPointer");
    }

}
